<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background-color: rgba(255,255,255,0)
            }
        </style>
    </head>
    <body>
        <div class="aui-tips">
            <i class="aui-iconfont aui-icon-info"></i>
            <div class="aui-tips-title aui-ellipsis-1" style="width:90%">
                可在 [我的] ➔ [设置] 中关闭贷款提醒选项。
            </div>
        </div>
    </body>
    <script type="text/javascript" src="../../script/api.js"></script>
    <script type="text/javascript" src="../../script/APICloud-rest-SHA1.js" ></script>
    <script type="text/javascript" src="../../script/aui-toast.js" ></script>
    <script type="text/javascript" src="../../script/aui-dialog.js" ></script>
    <script type="text/javascript">
        var client = new Resource("A6928874653494", "4B6C428C-24C2-F9E7-3175-F983922E8C8F");
        var toast = new auiToast();
        var dialog = new auiDialog();
        function openWinFrame(winName, options) {
            var delay = 0;
            if (api.systemType != 'ios') {
                delay = 300;
            }
            api.openWin({
                name : winName,
                url : './' + winName + '.html',
                bounces : false,
                delay : delay,
                slidBackEnabled : true,
                vScrollBarEnabled : false,
                animation : {
                    type : "movein", //动画类型（详见动画类型常量）
                    subType : "from_left", //动画子类型（详见动画子类型常量）
                    duration : delay //动画过渡时间，默认300毫秒
                },
                pageParam : options,
            });
        }

        function calcPayProgress(e) {
            var myDate = new Date();
            var year = parseInt(myDate.getFullYear());
            var month = parseInt(myDate.getMonth()) + 1;
            var day = parseInt(myDate.getDate());
            var oldYear = parseInt(e.validYear);
            var oldMonth = parseInt(e.validMonth);
            var oldDay = parseInt(e.validDay);
            if (e.payTypeIndex == 1) {
                var curDayCount = year * 365 + (month - 1) * 30 + day;
                var payDayCount = oldYear * 365 + (oldMonth - 1) * 30 + oldDay + e.loanPeriod;
                if (curDayCount > payDayCount) {
                    return 100.00;
                } else {
                    return 0;
                }
            } else {
                var payMonth = 0;
                yearDiff = year - oldYear;
                if (yearDiff > 0) {
                    payMonth = (yearDiff - 1) * 12 + (12 - oldMonth + 1) + (month - 1);
                } else if (yearDiff == 0) {
                    //alert("Date: " + year + " " + month + " " + day);
                    monthDiff = month - oldMonth;
                    if (monthDiff > 0) {
                        payMonth = monthDiff;
                    } else if (monthDiff < 0) {
                        return 0;
                    }
                } else if (yearDiff < 0) {
                    return 0;
                }
                if (day > oldDay) {
                    payMonth = payMonth + 1;
                }
                var PayRate = payMonth * 100.00 / e.loanPeriod;
                if (PayRate > 100.00) {
                    PayRate = 100.00;
                }
                return PayRate;
            }
            //alert("Date: " + oldYear + " " + oldMonth + " " + oldDay);
        }

        function checkRecentPay(loanListItem) {
            var alertRequired = 0;
            var myDate = new Date();
            var year = parseInt(myDate.getFullYear());
            var month = parseInt(myDate.getMonth()) + 1;
            var day = parseInt(myDate.getDate());
            var oldYear = parseInt(loanListItem.validYear);
            var oldMonth = parseInt(loanListItem.validMonth);
            var oldDay = parseInt(loanListItem.validDay);
            //alert("day: " + day + " oldDay: " + oldDay);
            var dayDiff = oldDay - day;
            if (loanListItem.payTypeIndex == 1) {
                var curDayCount = year * 365 + (month - 1) * 30 + day;
                var payDayCount = oldYear * 365 + (oldMonth - 1) * 30 + oldDay;
                var countDiff = payDayCount - curDayCount;
                if (countDiff > 0 && countDiff < 5) {
                    alertRequired = 1;
                    //alert(count);
                }
            } else {
                if (dayDiff > 0 && dayDiff < 5) {
                    alertRequired = 1;
                    //alert(count);
                }
            }
            return alertRequired;
        }

        function banLoanAlert() {
            var modelUserLoanRecord = client.Factory("userLoanRecord");
            modelUserLoanRecord.query({
                filter : {
                    "order" : "createdAt DESC",
                    "where" : {
                        "userId" : $api.getStorage('userID')
                    }
                }
            }, function(ret, err) {
                if (err) {
                    //处理错误 err
                    alert(JSON.stringify(err));
                } else {
                    //处理数据 ret
                    loanList = ret;
                    //alert(JSON.stringify(ret));
                    var count = 0;
                    var banAlertMonth = 0;
                    for (var i = 0; i < loanList.length; i++) {
                        var alertRequired = 0;
                        var calcRate = calcPayProgress(loanList[i]);
                        if (calcRate < 100.00) {
                            alertRequired = checkRecentPay(loanList[i]);
                            count = count + alertRequired;
                            if (alertRequired == 1) {
                                var myDate = new Date();
                                var year = parseInt(myDate.getFullYear());
                                var month = parseInt(myDate.getMonth()) + 1;
                                banAlertMonth = year * 12 + month;
                            }
                        }
                        modelUserLoanRecord.save({
                            "_id" : loanList[i].id
                        }, {
                            "$set" : {
                                "banAlertMonth" : banAlertMonth,
                            }
                        }, function(ret, err) {
                            if (err) {
                                //处理错误 err
                            } else {
                                //处理数据 ret
                                //alert(JSON.stringify(ret));
                            }
                        })
                    };
                    api.sendFrameToBack({
                        from : "alert_loan"
                    });
                }
            });
        }

        apiready = function() {
            api.parseTapmode();
            dialog.alert({
                title : "您有贷款还款日即将到来",
                msg : '需要查看吗？',
                buttons : ['稍候', '不再提示', '查看']
            }, function(ret) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        api.closeFrame({
                        });
                    } else if (ret.buttonIndex == 2) {
                        dialog.alert({
                            title : "提示",
                            msg : "可在 [我的] ➔ [近期还贷] 页面中查看",
                            buttons : ['确定']
                        }, function(ret) {
                            banLoanAlert();
                        });
                    } else if (ret.buttonIndex == 3) {
                        openWinFrame('my_recentpay_win', null);
                        banLoanAlert();
                    }
                    /*
                     dialog.alert({
                     title : "提示",
                     msg : "您点击了第" + ret.buttonIndex + "个按钮",
                     buttons : ['确定']
                     });
                     */
                }
            })
        };
    </script>
</html>